<?php
/**
 * 应收车商收款单
 * @author
 */

date_default_timezone_set("Asia/Shanghai");
set_time_limit(0);
if (isset($_SERVER['REQUEST_URI'])) {
    die('error');
}

if (!defined('API_ROOT_PATH')) {
    define('API_ROOT_PATH', dirname(__FILE__));
}

require(API_ROOT_PATH . '/../inc/common.php');
require(API_ROOT_PATH . '/../inc/Config.php');
require(API_ROOT_PATH . '/../inc/MysqliDb.php');

//初始化数据库链接
function init_db()
{
    $db = new MysqliDb ([
        'host' => MYSQLDB,
        'username' => MYSQLDB_USER,
        'password' => MYSQLDB_PWD,
        'db' => MYSQLDB_DATABASE,
        'port' => 3306,
        'prefix' => 'au_',
        'charset' => 'utf8'
    ]);
    return $db;
}

$db = init_db();

$data = array();
$time = date('Y-m-d H:i:s');
$last_id = 0;
$counter = 0;
while(true){
	$list = $db->where("pl_id", $last_id, ">")->get('proceeds_log',2000);
	if (empty($list)) {
		logUtil($counter . "条数据处理完成", 'INFO');
		exit;
	}

	foreach($list as $k => $val){
		$db->startTransaction();
		$data['collection_no'] = genBillsNo("SK", 01,$time);
		$status = $db->where('order_id',$val['order_id'])->getOne('order','dealer_pay_status');
		if($status == 0 || $status == 1 || $status == 2){
			$data['status'] = 0;
		}else if($status == 3){
			$data['status'] = 1;
		}else if($status == 4){
			$data['status'] = 2;
		}else if($status == 5){
			$data['status'] = 3;
		}
		$data['payment_type'] = 1;//车款应收
		if($val['pay_type'] == 1 || $val['pay_type'] == 2){
			$data['payment_method'] = 1;
		}else if($val['pay_type'] == 3){
			$data['payment_method'] = 2;
		}
		$data['amount'] = $val['amount'];
		$data['emp_id'] = $val['emp_id'];
		$data['emp_name'] = $db->where('emp_id',$val['emp_id'])->getOne('employee','real_name');
		$data['create_time'] = $val['createtime'];
		$data['update_time'] = $val['verify_time'];

		$collectionId = $db->insert('collection',$data);
		if($collectionId){
			$orderCollectionData = [
				'payment_type' => 1,
				'order_id' => $val['order_id'],
				'collection_id' => $collectionId,
				'createtime' => date('Y-m-d H:i:s',time()),
			];
			$result = $db->insert('order_collections',$data);
			if($result){
				$db->commit();
				logUtil('拍单',$val[order_id],'生成付款单及拍单关系成功');
			}else{
				$db->rollback();
				logUtil('拍单',$val[order_id],'生成付款单及拍单关系失败','ERR');
			}
		}else{
			$db->rollback();
			logUtil('拍单',$val[order_id],'生成付款单失败','ERR');
		}
		$last_id = $item['pl_id'];
		$counter++;
	}
}

/**
 * 产生固定长度的随机字符串
 * @param $len
 * @param null $chars
 * @return string
 */
function genRandomChars($len, $chars = null)
{
    if (is_null($chars)) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    }
    mt_srand(10000000 * (double)microtime());
    for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; $i++) {
        $str .= $chars[mt_rand(0, $lc)];
    }
    return $str;
}

/**
 * 产生一个四位的随机字符串
 * @return string
 */
function gen4Chars()
{
    $strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $numbers = "0123456789";
    $randomString = genRandomChars(3);
    if (ctype_digit($randomString)) {
        $randomString = $randomString . genRandomChars(1, $strChars);
    } elseif (ctype_alpha($randomString)) {
        $randomString = $randomString . genRandomChars(1, $numbers);
    } else {
        $randomString = $randomString . genRandomChars(1);
    }
    return $randomString;
}

/**
 * 产生一个单据编号
 * @param $type
 * @param $subType
 * @param $date
 * @return string
 */
function genBillsNo($type, $subType, $date)
{
    if ($subType < 10) $subType = "0" . $subType;
    $date = date("Ymd",strtotime($date));
    return $type . $subType . $date . gen4Chars();
}